package com.usermanage.SecurityConfig;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * Created by 過客 on 2018/3/27
 */
public class AuthenticationAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse resp, AccessDeniedException e) throws IOException, ServletException {
        resp.setStatus(HttpServletResponse.SC_FORBIDDEN);

//        throw new MyException("权限不足，请联系管理员!");
//        throw new RuntimeException("权限不足，请联系管理员!");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");

        resp.sendRedirect("/exception/index.page?err="+ URLEncoder.encode("权限不足，请联系管理员!", "UTF-8"));

//        resp.getWriter().println("{\"ok\":0,\"msg\":\"权限不足，请联系管理员!\"}");
    }
}
